/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.batavia.inter.impl;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.util.Cookie;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import org.batavia.domain.User;
import org.batavia.inter.InterfaceLogin;
import org.joda.time.DateTime;

public class LoginImpl implements InterfaceLogin {

    // public static WebClient client;
    // public static HtmlPage page;
    WebClient client;
    public static HtmlPage page2;
    private User user;
    private boolean exp;
    DateTime expire = new DateTime().plusDays(30);
    private Cookie cookie = new Cookie("web.batavia-air.com", "PHPSESSID", "061d4be2ce0174041302a5a4d2614fef", "/", expire.toDate(), false);
    // private boolean cek;

    public boolean cekLogin() {

        if (client.getCookieManager().getCookies() == null) {
            client.getCookieManager().addCookie(cookie);
            return true;
        } else {
            return false;

        }
    }

    public void login() {
        if (client.getCookieManager().getCookies() == null) {
            client.getCookieManager().addCookie(cookie);

        } else {
            client.getCookieManager().addCookie(cookie);

        }

    }

    /*
    public boolean cekLogin(Cookie cookie) {

    if (cookie != null) {
    return true;
    } else {

    return false;
    }
    }
     */
    public User login(String username, String password) {
        try {
            client = new WebClient(BrowserVersion.getDefault());
            client.setJavaScriptEnabled(false);
            client.setThrowExceptionOnScriptError(false);
            client.setCssEnabled(true);

            HtmlPage page = client.getPage("http://web.batavia-air.com/MyPage/login.php");


            /*
            if (cekLogin() == false) {
            client.getCookieManager().addCookie(cookie);
            HtmlForm form = page.getFormByName("loginForm");

            form.getInputByName("useridmp").setValueAttribute(username);
            form.getInputByName("passwordmp").setValueAttribute(password);
            HtmlSubmitInput button = form.getInputByValue("sign in!");
            page2 = button.click();

            } else {

            HtmlForm form = page.getFormByName("loginForm");
            form.getInputByName("useridmp").setValueAttribute(username);
            form.getInputByName("passwordmp").setValueAttribute(password);
            HtmlSubmitInput button = form.getInputByValue("sign in!");
            page2 = button.click();

            }
            //System.out.println(page2.asText());
            //  System.out.println(page2.getUrl());

            // client.closeAllWindows();*/

            login();
            HtmlForm form = page.getFormByName("loginForm");
            form.getInputByName("useridmp").setValueAttribute(username);
            form.getInputByName("passwordmp").setValueAttribute(password);
            HtmlSubmitInput button = form.getInputByValue("sign in!");
            page2 = button.click();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user;

    }
}
